"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 兼容日期函数
Case Name   : adddate()函数第一参数指定time类型输入time类型参数测试边界值，第二参数输入正确
Create At   : 2023.03.15
@zou_jialiang0501129488
Description :
    1、创建兼容b库并查询插件是否加载成功
    2、adddate()函数第一参数指定time类型输入time类型参数测试边界值，第二参数输入正确
    3、清理环境
Expect      :
    1、成功，插件查询成功
    2、成功
    3、清理成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class CompatibilityTest01(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.sh_primary = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_adddate_0052'

    def test_adddate(self):
        text = '-----step1:创建兼容b库库并查询插件;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists {self.db_name};" \
                  f"create database {self.db_name} dbcompatibility ='B';"
        self.log.info(sql_cmd)
        sql_res = self.sh_primary.execut_db_sql(sql_cmd)
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS,
                      sql_res, '兼容B库创建失败')

        sql_cmd1 = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd1)
        sql_res1 = self.sh_primary.execut_db_sql(sql_cmd1, dbname=self.db_name)
        self.log.info(sql_res1)
        self.assertIn('dolphin', sql_res1, '查询失败')

        text = '-----step2:adddate()函数第一参数指定time类型输入time类型参数测试边界值，第二参数输入正确;' \
               'expect:成功-----'
        self.log.info(text)
        sql_cmd2 = f"select adddate(time'838:59:59', -1);" \
                   f"select adddate(time'-838:59:59', 3);"
        self.log.info(sql_cmd2)
        res = self.sh_primary.execut_db_sql(sql_cmd2, dbname=self.db_name)
        self.log.info(res)
        msg_lis = ['814:59:59', '-766:59:59']
        for msg in msg_lis:
            self.assertIn(msg, res, '日期函数查询失败')

    def tearDown(self):
        text = '-----step3:清理环境;expect:成功-----'
        self.log.info(text)
        sql_cmd4 = f"drop database if exists {self.db_name};"
        self.log.info(sql_cmd4)
        sql_res4 = self.sh_primary.execut_db_sql(sql_cmd4)
        self.log.info(sql_res4)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS,
                      sql_res4, '清理环境失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
